package com.sie.zhq.module;

import com.sie.zhq.tool.ByteTool;

/**
 * Created by zhq on 2016/8/19.
 */
public class EMVParam {

    private byte[] MerchName = new byte[64];
    private byte[] MerchCateCode = new byte[2];
    private byte[] MerchId = new byte[15];
    private byte[] TermId = new byte[8];
    private byte TerminalType;
    private byte[] Capability = new byte[3];
    private byte[] ExCapability = new byte[5];
    private byte TransCurrExp;
    private byte ReferCurrExp;
    private byte[] ReferCurrCode = new byte[2];
    private byte[] CountryCode = new byte[2];
    private byte[] TransCurrCode = new byte[2];
    private int ReferCurrCon;
    private byte TransType;
    private byte ForceOnline;
    private byte bExceptionFile;
    private byte bBatchCapture;
    private byte bSupportAdvices;
    private byte POSEntryMode;
    private byte PosKernelType;
    private byte GetDataPIN;
    private byte SurportPSESel;
    private byte[] TermTransQuali = new byte[4];
    private byte ECTSI;
    private byte EC_bTermLimitCheck;
    private int EC_TermLimit;
    private byte CL_bStatusCheck;
    private int CL_FloorLimit;
    private int CL_TransLimit;
    private int CL_CVMLimit;

    public EMVParam() {

    }

    public EMVParam(byte[] merchName, byte[] merchCateCode, byte[] merchId,
                    byte[] termId, byte terminalType, byte[] capability,
                    byte[] exCapability, byte transCurrExp, byte referCurrExp,
                    byte[] referCurrCode, byte[] countryCode, byte[] transCurrCode,
                    int referCurrCon, byte transType, byte forceOnline,
                    byte bExceptionFile, byte bBatchCapture, byte bSupportAdvices,
                    byte pOSEntryMode, byte posKernelType, byte getDataPIN,
                    byte surportPSESel, byte[] termTransQuali, byte eCTSI,
                    byte eC_bTermLimitCheck, int eC_TermLimit, byte cL_bStatusCheck,
                    int cL_FloorLimit, int cL_TransLimit, int cL_CVMLimit) {
        super();
        MerchName = merchName;
        MerchCateCode = merchCateCode;
        MerchId = merchId;
        TermId = termId;
        TerminalType = terminalType;
        Capability = capability;
        ExCapability = exCapability;
        TransCurrExp = transCurrExp;
        ReferCurrExp = referCurrExp;
        ReferCurrCode = referCurrCode;
        CountryCode = countryCode;
        TransCurrCode = transCurrCode;
        ReferCurrCon = referCurrCon;
        TransType = transType;
        ForceOnline = forceOnline;
        this.bExceptionFile = bExceptionFile;
        this.bBatchCapture = bBatchCapture;
        this.bSupportAdvices = bSupportAdvices;
        POSEntryMode = pOSEntryMode;
        PosKernelType = posKernelType;
        GetDataPIN = getDataPIN;
        SurportPSESel = surportPSESel;
        TermTransQuali = termTransQuali;
        ECTSI = eCTSI;
        EC_bTermLimitCheck = eC_bTermLimitCheck;
        EC_TermLimit = eC_TermLimit;
        CL_bStatusCheck = cL_bStatusCheck;
        CL_FloorLimit = cL_FloorLimit;
        CL_TransLimit = cL_TransLimit;
        CL_CVMLimit = cL_CVMLimit;
    }


    public void parseFromBytes(byte[] EMV_PARAM) {

        System.arraycopy(EMV_PARAM, 0, MerchName, 0, 64);

        System.arraycopy(EMV_PARAM, 64, MerchCateCode, 0, 2);

        System.arraycopy(EMV_PARAM, 64 + 2, MerchId, 0, 15);

        System.arraycopy(EMV_PARAM, 64 + 2 + 15, TermId, 0, 8);

        TerminalType = EMV_PARAM[64 + 2 + 15 + 8];

        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9, Capability, 0, 3);

        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3, ExCapability, 0, 5);

        TransCurrExp = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 5];

        ReferCurrExp = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 6];

        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7, ReferCurrCode, 0, 2);

        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 2, CountryCode, 0, 2);

        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 2 + 2, TransCurrCode, 0, 2);

        byte[] b = new byte[4];
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 6, b, 0, 4);
        ReferCurrCon = ByteTool.BytesToInt(b, 0);

        //设置交易类型
        TransType = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 10];
        //强制联机交易
        ForceOnline = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 11];
        //异常文件是否支持
        bExceptionFile = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 12];
        //支持批捕获
        bBatchCapture = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 13];

        //是否支持通知
        bSupportAdvices = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 14];
        //Pos Entry Mode
        POSEntryMode = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 15];

        //内核类型0 -EMV KERNEL 1-PBOC KERNEL
        PosKernelType = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 16];
        //密码检测前是否读重试次数
        GetDataPIN = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 17];
        //是否支持PSE选择方式
        SurportPSESel = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 18];

        //终端交易限制 4字节
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 19, TermTransQuali, 0, 4);

        //电子现金终端支持指示器
        ECTSI = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 23];

        //是否支持终端交易限额
        EC_bTermLimitCheck = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 24];

        //终端交易限额
        b = new byte[4];
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 25, b, 0, 4);
        EC_TermLimit = ByteTool.BytesToInt(b, 0);

        //是否支持qPBOC状态检查
        CL_bStatusCheck = EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 29];
        //非接触终端最低限额
        b = new byte[4];
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 30, b, 0, 4);
        CL_FloorLimit = ByteTool.BytesToInt(b, 0);

        b = new byte[4];
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 34, b, 0, 4);
        CL_TransLimit = ByteTool.BytesToInt(b, 0);
        // 非接触终端CVM限额
        b = new byte[4];
        System.arraycopy(EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 38, b, 0, 4);

        CL_CVMLimit = ByteTool.BytesToInt(b, 0);


    }

    public byte[] toBytes() {
        byte[] EMV_PARAM = new byte[142];

        // merchant name 64字节

        System.arraycopy(MerchName, 0, EMV_PARAM, 0, MerchName.length);

        // 参数2个字节 商户类别码(没要求可不设置)
        System.arraycopy(MerchCateCode, 0, EMV_PARAM, 64, 2);

        //商户标志(商户号) 15字节
        System.arraycopy(MerchId, 0, EMV_PARAM, 64 + 2, 15);

        //终端标志(POS号) 8字节
        System.arraycopy(TermId, 0, EMV_PARAM, 64 + 2 + 15, 8);

        //终端类型(详见终端规范表A.1)
        EMV_PARAM[64 + 2 + 15 + 8] = TerminalType;

        //终端性能 3字节
        System.arraycopy(Capability, 0, EMV_PARAM, 64 + 2 + 15 + 9, 3);
        //终端扩展性能 5字节
        System.arraycopy(ExCapability, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3, 5);
        //交易货币代码指数
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 5] = TransCurrExp;
        // 参考货币指数
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 6] = ReferCurrExp;
        //参考货币代码 2字节
        System.arraycopy(ReferCurrCode, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7, 2);
        // 终端国家代码
        System.arraycopy(CountryCode, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 2, 2);
        // 交易货币代码
        System.arraycopy(TransCurrCode, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 2 + 2, 2);
        //参考货币代码和交易代码的转换系数
        byte[] b = ByteTool.intTo4bytes(ReferCurrCon);
        System.arraycopy(b, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 6, 4);
        //设置交易类型
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 10] = TransType;
        //强制联机交易
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 11] = ForceOnline;
        //异常文件是否支持
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 12] = bExceptionFile;
        //支持批捕获
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 13] = bBatchCapture;

        //是否支持通知
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 14] = bSupportAdvices;
        //Pos Entry Mode
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 15] = POSEntryMode;
        //内核类型0 -EMV KERNEL 1-PBOC KERNEL
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 16] = PosKernelType;
        //密码检测前是否读重试次数
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 17] = GetDataPIN;
        //是否支持PSE选择方式
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 18] = SurportPSESel;
        //终端交易限制 4字节
        System.arraycopy(TermTransQuali, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 19, 4);

        //电子现金终端支持指示器
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 23] = ECTSI;

        //是否支持终端交易限额
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 24] = EC_bTermLimitCheck;
        //终端交易限额
        b = ByteTool.intTo4bytes(EC_TermLimit);
        System.arraycopy(b, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 25, 4);

        //是否支持qPBOC状态检查
        EMV_PARAM[64 + 2 + 15 + 9 + 3 + 7 + 29] = CL_bStatusCheck;
        //非接触终端最低限额
        b = ByteTool.intTo4bytes(CL_FloorLimit);
        System.arraycopy(b, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 30, 4);

        //非接触终端交易限额
        b = ByteTool.intTo4bytes(CL_TransLimit);
        System.arraycopy(b, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 34, 4);

        // 非接触终端CVM限额
        b = ByteTool.intTo4bytes(CL_CVMLimit);
        System.arraycopy(b, 0, EMV_PARAM, 64 + 2 + 15 + 9 + 3 + 7 + 38, 4);

        return EMV_PARAM;
    }

    public void setMerchName(byte[] merchName) {
        MerchName = merchName;
    }

    public void setMerchCateCode(byte[] merchCateCode) {
        MerchCateCode = merchCateCode;
    }

    public void setMerchId(byte[] merchId) {
        MerchId = merchId;
    }

    public void setTermId(byte[] termId) {
        TermId = termId;
    }

    public void setTerminalType(byte terminalType) {
        TerminalType = terminalType;
    }

    public void setCapability(byte[] capability) {
        Capability = capability;
    }

    public void setExCapability(byte[] exCapability) {
        ExCapability = exCapability;
    }

    public void setTransCurrExp(byte transCurrExp) {
        TransCurrExp = transCurrExp;
    }

    public void setReferCurrExp(byte referCurrExp) {
        ReferCurrExp = referCurrExp;
    }

    public void setReferCurrCode(byte[] referCurrCode) {
        ReferCurrCode = referCurrCode;
    }

    public void setCountryCode(byte[] countryCode) {
        CountryCode = countryCode;
    }

    public void setTransCurrCode(byte[] transCurrCode) {
        TransCurrCode = transCurrCode;
    }

    public void setReferCurrCon(int referCurrCon) {
        ReferCurrCon = referCurrCon;
    }

    public void setTransType(byte transType) {
        TransType = transType;
    }

    public void setForceOnline(byte forceOnline) {
        ForceOnline = forceOnline;
    }

    public void setbExceptionFile(byte bExceptionFile) {
        this.bExceptionFile = bExceptionFile;
    }

    public void setbBatchCapture(byte bBatchCapture) {
        this.bBatchCapture = bBatchCapture;
    }

    public void setbSupportAdvices(byte bSupportAdvices) {
        this.bSupportAdvices = bSupportAdvices;
    }

    public void setPOSEntryMode(byte POSEntryMode) {
        this.POSEntryMode = POSEntryMode;
    }

    public void setPosKernelType(byte posKernelType) {
        PosKernelType = posKernelType;
    }

    public void setGetDataPIN(byte getDataPIN) {
        GetDataPIN = getDataPIN;
    }

    public void setSurportPSESel(byte surportPSESel) {
        SurportPSESel = surportPSESel;
    }

    public void setTermTransQuali(byte[] termTransQuali) {
        TermTransQuali = termTransQuali;
    }

    public void setECTSI(byte ECTSI) {
        this.ECTSI = ECTSI;
    }

    public void setEC_bTermLimitCheck(byte EC_bTermLimitCheck) {
        this.EC_bTermLimitCheck = EC_bTermLimitCheck;
    }

    public void setEC_TermLimit(int EC_TermLimit) {
        this.EC_TermLimit = EC_TermLimit;
    }

    public void setCL_bStatusCheck(byte CL_bStatusCheck) {
        this.CL_bStatusCheck = CL_bStatusCheck;
    }

    public void setCL_FloorLimit(int CL_FloorLimit) {
        this.CL_FloorLimit = CL_FloorLimit;
    }

    public void setCL_TransLimit(int CL_TransLimit) {
        this.CL_TransLimit = CL_TransLimit;
    }

    public void setCL_CVMLimit(int CL_CVMLimit) {
        this.CL_CVMLimit = CL_CVMLimit;
    }

    public byte[] getMerchName() {
        return MerchName;
    }

    public byte[] getMerchCateCode() {
        return MerchCateCode;
    }

    public byte[] getMerchId() {
        return MerchId;
    }

    public byte[] getTermId() {
        return TermId;
    }

    public byte getTerminalType() {
        return TerminalType;
    }

    public byte[] getCapability() {
        return Capability;
    }

    public byte[] getExCapability() {
        return ExCapability;
    }

    public byte getTransCurrExp() {
        return TransCurrExp;
    }

    public byte getReferCurrExp() {
        return ReferCurrExp;
    }

    public byte[] getReferCurrCode() {
        return ReferCurrCode;
    }

    public byte[] getCountryCode() {
        return CountryCode;
    }

    public byte[] getTransCurrCode() {
        return TransCurrCode;
    }

    public int getReferCurrCon() {
        return ReferCurrCon;
    }

    public byte getTransType() {
        return TransType;
    }

    public byte getForceOnline() {
        return ForceOnline;
    }

    public byte getbExceptionFile() {
        return bExceptionFile;
    }

    public byte getbBatchCapture() {
        return bBatchCapture;
    }

    public byte getbSupportAdvices() {
        return bSupportAdvices;
    }

    public byte getPOSEntryMode() {
        return POSEntryMode;
    }

    public byte getPosKernelType() {
        return PosKernelType;
    }

    public byte getGetDataPIN() {
        return GetDataPIN;
    }

    public byte getSurportPSESel() {
        return SurportPSESel;
    }

    public byte[] getTermTransQuali() {
        return TermTransQuali;
    }

    public byte getECTSI() {
        return ECTSI;
    }

    public byte getEC_bTermLimitCheck() {
        return EC_bTermLimitCheck;
    }

    public int getEC_TermLimit() {
        return EC_TermLimit;
    }

    public byte getCL_bStatusCheck() {
        return CL_bStatusCheck;
    }

    public int getCL_FloorLimit() {
        return CL_FloorLimit;
    }

    public int getCL_TransLimit() {
        return CL_TransLimit;
    }

    public int getCL_CVMLimit() {
        return CL_CVMLimit;
    }
}
